package com.wandian.texas.net;

import com.wandian.util.Log;
import com.wandian.util.StringUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;

/**
 * @date : 2012-9-25, 17:03:26
 * @author: long
 * @email :108232706
 * @qq.com
 * @description: SessionObject类
 */
public class SessionObject implements ISessionObject {

    private Socket socket;
    private boolean started;
    private int count;
    public byte[] readBuffer = new byte[1024 * 5];
    public byte[] sendBuffer = new byte[1024 * 5];

   
    public int byteArrayToInt(byte[] b, int start, int offset) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i + start + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public void write(int length) throws SocketException, IOException {
        OutputStream writeStream = socket.getOutputStream();
        //sendBuffer.
        if (isStarted()) {
            writeStream.write(sendBuffer, 0, length);
        }
    }

    @SuppressWarnings("empty-statement")
    public void write(String msg) throws SocketException, IOException {
        OutputStream writeStream = socket.getOutputStream();
        //sendBuffer.
        if (isStarted()) {
//               byte[] heads =[0xab,0x71,0x1e,0x0,0x0,0x0,0x8,0x0,0x1,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x3,0x0,0x0,0x0];
            byte[] msgbytes = msg.getBytes("utf-8");
            printBytes("发送", msgbytes, msgbytes.length);
            writeStream.write(msgbytes, 0, msgbytes.length);
        }
    }

    public void printBytes(String msg, byte[] bytes, int length) {
        String result = msg + " length:" + length + " content:" + StringUtil.byte2hex(bytes, length);
        Log.info(result);
    }

    /**
     * @return the socket
     */
    public Socket getSocket() {
        return socket;
    }

    /**
     * @param socket the socket to set
     */
    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    /**
     * @return the started
     */
    public boolean isStarted() {
        return started;
    }

    /**
     * @param started the started to set
     */
    public void setStarted(boolean started) {
        this.started = started;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    /**
     * @param count the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @return the count
     */
    public void addtCount(int value) {
        count += value;
    }

    public void printSocket() throws SocketException {
        System.out.println("remote addr :" + socket.getRemoteSocketAddress());
        System.out.println("local addr :" + socket.getLocalSocketAddress());
        System.out.println("tcp delay : " + socket.getTcpNoDelay());
        System.out.println("so linger : " + socket.getSoLinger());
        System.out.println("time out milliseconds : "
                + socket.getSoTimeout());
        System.out.println("receive buffer size : "
                + socket.getReceiveBufferSize());
        System.out.println("send buffer size : "
                + socket.getSendBufferSize());
        System.out.println("keep alive : " + socket.getKeepAlive());
        System.out
                .println("process urgent data : " + socket.getOOBInline());
        System.out.println("reuse address : " + socket.getReuseAddress());
        System.out.println("traffic class : " + socket.getTrafficClass());
    }
}
